<?php
/**
 * Created by PhpStorm.
 * User: ma
 * Date: 2018/1/25
 * Time: 14:54
 */

namespace app\restaurant\controller;

use app\common\model\User as UserModel;
use app\restaurant\model\DcAddress as DcAddressModel;
use app\restaurant\model\DcCollect as DcCollectModel;

define("USERSHOPLOGOURL",'https://cs.rongbaiwei.com/');

class Mine extends Base
{
    /**
     * 我的收藏-获取收藏列表
     */
    public function getMyCollection(){
        $request=$this->request;
//        $data=$this->request->param('tok_key');
//        $unionid=$this->get_unionid($data);
//        if($unionid === false){
//            return $this->getOutput(400,'token无效');
//        }
        $jing=$request->param('jing');
        $wei=$request->param('wei');
        $unionid=$request->param('unionid');
        if(!$unionid){
            return $this->getOutput(400,'token无效');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->field('id')->find()->toArray();
//        var_dump($user);
        $res=db('dc_collect')
            ->alias('dct')
            ->leftJoin('shops sp','sp.sp_id=dct.collect_shop_id')
            ->where('dct.collect_user_id',$user['id'])
            ->field('sp_id,sp_name,sp_logo_url,sp_longitude,sp_latitude,sp_area_id')
            ->order('dct.collect_time','desc')
            ->select();
        foreach ($res as $k => $v) {
            $res[$k]['range'] = ceil(getdistance($v['sp_latitude'],$v['sp_longitude'],$wei,$jing)*1000);
        }
        foreach ($res as $k => $v) {
            if($res[$k]['range'] >= 1000){
                $res[$k]['range'] = ($v['range']/1000).'千米';
            }else if($res[$k]['range'] == 0){
                $res[$k]['range'] = '小于50米';
            }else{
                $res[$k]['range'] = $v['range'].'米';
            }
        }
        static $arr=array();
        foreach ($res as $k=>$v){
            foreach ($v as $key=>$value){

                if($key=='sp_logo_url'){
                    $arr[$k][$key]=USERSHOPLOGOURL.$value;
                }elseif($key=='sp_area_id'){
                    $arr[$k][$key]=db('area')->where('id',$value)->field('name')->find();
                }
                else{
                    $arr[$k][$key]=$value;
                }
            }

        }

        if(!$res){
            return $this->getOUtput(404,'失败',[]);

        }
        //$res2=array_merge($arr,$arr2);
        return $this->getOUtput(200,'成功',$arr);



    }
    /**
     * 我的收藏-用户添加收藏
     */
    public function addCollection(){
        $request=$this->request;
//        $data=$this->request->param('tok_key');
//        $unionid=$this->get_unionid($data);
//        if($unionid === false){
//            return $this->getOutput(400,'token无效');
//        }
        $unionid=$request->param('unionid');
        if(!$unionid){
            return $this->getOutput(400,'token无效');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->field('id')->find()->toArray();

        $updateData['collect_shop_id']=$request->param('shop_id');
        $updateData['collect_time']=time();
        $updateData['collect_user_id']=$user;

        if(empty($updateData['collect_shop_id'])){
            return $this->getOUtput(401,'失败','数据不能为空');

        }
        $model2=new DcCollectModel;
        $res=$model2->save($updateData);

        if(!$res){
            return $this->getOUtput(404,'失败','');

        }
        return $this->getOUtput(200,'成功','');



    }

    /**
     * 添加地址
     */
    public function addAddress(){
        $request=$this->request;
//        $data=$this->request->param('tok_key');
//        $unionid=$this->get_unionid($data);
//        if($unionid === false){
//            return $this->getOutput(400,'token无效');
//        }
       // return $this->getOUtput(404,'失败',$request->param());

        $address=$request->param('address');
        // return $this->getOUtput(404,'失败',9);
//        var_dump($request->param());die;
        $unionid=$request->param('unionid');
        if(!$unionid){
            return $this->getOutput(400,'token无效');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->field('id')->find()->toArray();
        $updateData['as_mobile']=$request->param('phone');
        $updateData['as_name']=$request->param('name');
        $updateData['as_desc']=$request->param('description');
        $updateData['as_address']=$address.$request->param('detail');
        $updateData['as_create_time']=time();
        $updateData['as_user_id']=$user['id'];

       // return $this->getOutput(400,'token无效',$updateData);
        if(empty($updateData['as_name']&&$updateData['as_mobile']&&$updateData['as_address'])){
            return $this->getOUtput(401,'失败','数据不能为空');

        }
        $model2=new DcAddressModel;
//
//        return $this->getOutput(400,'token无效',$updateData);
        $res=$model2->save($updateData);

        if(!$res){
            return $this->getOUtput(404,'失败','');

        }
        return $this->getOUtput(200,'成功','');



    }
    /**
     * 获取收货地址
     */
    public function getAddress(){
        $request=$this->request;
//        $data=$this->request->param('tok_key');
//        $unionid=$this->get_unionid($data);
//        if($unionid === false){
//            return $this->getOutput(400,'token无效');
//        }
        $unionid=$request->param('unionid');
        if(!$unionid){
            return $this->getOutput(400,'token无效');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->field('id')->find()->toArray();
        $model2=new DcAddressModel;
        $res=$model2->where('as_user_id',$user['id'])->field('as_name,as_mobile,as_address,as_create_time')->select()->toArray();

        if(!$res){
            return $this->getOUtput(404,'失败',[]);

        }
        return $this->getOUtput(200,'成功',$res);


    }
    /**
     * 优惠券
     */
    public function coupons(){
        $unionid = $this->request->param('unionid');
//        $key = 123;
        if($unionid){
            if(!$unionid){
                return $this->getOutput(4000,'unionid不能为空！');
            }
//            $unionid = 1231321;
            // 用户ID
            $model = new UserModel();
            $user = db('user')->field('id')->where('unionid',$unionid)->find();
            // 用户红包余额 ua_coupons/100
            $account = db('user_account')->field('ua_coupons')->where('ua_user',$user['id'])->find();
            $data['account'] = sprintf('%.2f',$account['ua_coupons']/100);
            // 红包明细
            $coupons = db('coupons')
                ->alias('c')
                ->leftJoin('shops s','c.cp_shop_id=s.sp_id')
                ->field('c.cp_price,c.cp_create_time,s.sp_name,s.sp_logo_url')
                ->where('cp_user_id',$user['id'])
                ->select();
            foreach($coupons as $k=>$v){
                $coupons[$k]['cp_price'] = sprintf('%.2f',$v['cp_price']/100);
                if($v['cp_create_time']) $coupons[$k]['cp_create_time'] = date('Y-m-d H:i:s',$v['cp_create_time']);
                if($v['sp_logo_url']) $coupons[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
            }
            $data['coupons'] = $coupons;
            return $this->getOutput('200','ok',$data);
        }else{
            return $this->getOutput(404,'获取失败');
        }

    }
    /**
     * 购物车
     */
    public function cart(){

    }



}